package com.itxiaowu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.google.gson.Gson;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.constant.RedisConstant;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Setmeal;
import com.itxiaowu.service.SetmealService;
import com.itxiaowu.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 文件上传  --> 实现图片上传
     */
    @RequestMapping("/upload.do")
    public Result uploadPic(@RequestParam(name = "imgFile") MultipartFile imgFile){

        //1；获得图片名
        String oldName = imgFile.getOriginalFilename();
        String suffic = oldName.substring(oldName.lastIndexOf("."));

        //2: 设置新的文件名
        String newName = UUID.randomUUID().toString() + oldName;

        try {
            //3：调用文件上传的方法进行文件上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),newName);

            //将文件名放入jedis中, 后面进行删除垃圾图片操作
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,newName);

            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,newName);
        } catch (IOException e) {
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    /**
     *  添加操作
     */
    @RequestMapping("/addsetmeal.do")
//    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result addSetmeal(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){

        try{
            setmealService.addSetmeal(checkgroupIds,setmeal);

            /*放入jedis进行删除操作*/
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());

            return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        }catch(Exception e){
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

    }

    /**
     * 进行分页查询
     */
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return setmealService.findPage(queryPageBean);
    }

    /**
     * 根据套餐id查询套餐信息
     */
    @RequestMapping("/getsetmealById.do")
    public Result getSetmealById(Integer id){

        try{

            Jedis jedis = jedisPool.getResource();
            String setmealById = jedis.get("setmeal" + id);

            Setmeal setmeal = null;
            if(setmealById != null) {
                setmeal = new Gson().fromJson(setmealById, Setmeal.class);
            }else{
                setmeal = setmealService.findById(id);
                String setmealJson = new Gson().toJson(setmeal);
                jedis.set("setmeal" + id, setmealJson);
            }

            jedis.close();
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }

    }

    /**
     * 编辑： 根据套餐id查询 检查组id值
     */
    @RequestMapping("/getcheckgroupIdsBySetmealId.do")
    public Result getCheckGroupIdBySetmealId(Integer id){
        try {
            List<Integer> checkgroupIds = setmealService.getcheckgroupIdsBySetmealId(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     *  编辑: 将数据修改
     */
    @RequestMapping("/editSetmeal.do")
    //    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result editSetmeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){

        try{
            setmealService.editSetmeal(setmeal,checkgroupIds);
            // 清空套餐信息
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal" + setmeal.getId());
            jedis.del("setmealAll");
            jedis.close();

            return new Result(true,"编辑套餐成功");
        }catch (Exception e){
            return new Result(false,"编辑套餐失败");
        }
    }

    /**
     *  删除套餐信息
     */
    @RequestMapping("/delete.do")
    public Result delete(Integer id){

        try{
            setmealService.delete(id);

            // 清空套餐信息
            Jedis jedis = jedisPool.getResource();
            jedis.del("setmeal" + id);
            jedis.del("setmealAll");
            jedis.close();
            return new Result(true,"删除套餐成功");
        }catch (Exception e){
            return new Result(false,"删除套餐失败");
        }

    }
}
